/**
 * Created by diogoxiang on 2017年6月16日19:03:35.
 * modify  : 2019-11-6 15:35:3
 * version : 0.0.2 版本
 * anthor : diogoxiang
 *  CMD :  可自动刷新
 */
const gulp = require('gulp')
const template = require('gulp-art-tpl')
var htmlmin = require('gulp-htmlmin');
var gulpCopy = require('gulp-copy');
const rimraf = require('rimraf');
var uglify = require('gulp-uglify');
var pump = require('pump');

var cleanCSS = require('gulp-clean-css');

// @ts-ignore
var watch = require('gulp-watch');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var reload = browserSync.reload;


var source = 'src/'
var dist = 'dist/';



gulp.task('clean', function () {
    rimraf.sync(`${dist}/*`);
})


gulp.task('compress', function (cb) {
    pump([
            gulp.src(`${source}/*.js`),
            uglify(),
            gulp.dest(dist)
        ],
        cb
    );
});

gulp.task('copy', function () {
    gulp.src(`${source}/static/**`)
        .pipe(gulp.dest(`${dist}/static`));

    gulp.src(`${source}/libs/**`)
        .pipe(gulp.dest(`${dist}/libs`));

    gulp.src(`${source}/js/**`)
        .pipe(gulp.dest(`${dist}/js`));

    // gulp.src(`${source}/css/*.css`)
    //     .pipe(cleanCSS({
    //         compatibility: 'ie8'
    //     }))
    //     .pipe(gulp.dest(`${dist}/css`));

    // gulp.src([`${source}/*.png`, `${source}/*.jpg`, `${source}/*.gif`, `${source}/*.svg`])
    //     .pipe(gulp.dest(dist))

    // gulp.src(`${source}/*.js`)
    //     .pipe(uglify())
    //     .pipe(gulp.dest(dist));

})



gulp.task('html', function () {
    gulp.src('./src/*.html')
        .pipe(template({
            NODE_ENV: 'production',
            author: 'Mervin'
        }))
        .pipe(gulp.dest(dist))
})


// gulp.task('watch', function () {
//     w('./src/**/*.html', 'html');
//     // w('./src/js/**', 'js_main');
//     // w('./src/libs/**/*.js', 'js_libs');
//     // w('./src/css/**', 'css_main');
//     // w('./src/libs/**/*.css', 'css_libs');
//     // w('./src/images/**', 'images');

//     function w(path, task) {
//         watch(path, function () {
//             gulp.start(task);
//             // // browserSync.reload();
//         });
//     }
// });

gulp.task('serve', ['sass'], function () {

    browserSync.init({
        server: "./"
    });

    gulp.watch("./src/scss/**/*.scss", ['sass']);
    gulp.watch('./src/**/*.html', ['html']).on('change', reload);
    gulp.watch("./src/**/*.js", ['copy']).on('change', reload);
    // gulp.watch("app/*.html").on('change', reload);
});


// scss编译后的css将注入到浏览器里实现更新
gulp.task('sass', function () {
    return gulp.src("./src/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest(dist + "/css"))
        .pipe(reload({
            stream: true
        }));
});

gulp.task('default', ['copy', 'serve']);